package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.geekidea.springbootplus.system.constant.TaskStatusConstant;
import io.geekidea.springbootplus.system.entity.Task;
import io.geekidea.springbootplus.system.mapper.TaskMapper;
import io.geekidea.springbootplus.system.service.TaskService;
import io.geekidea.springbootplus.system.param.TaskPageParam;
import io.geekidea.springbootplus.system.vo.TaskQueryVo;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 *  服务实现类
 *
 * @author miko
 * @since 2020-10-25
 */
@Slf4j
@Service
public class TaskServiceImpl extends BaseServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveTask(Task task) throws Exception {
        return super.save(task);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTask(Task task) throws Exception {
        return super.updateById(task);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTask(Long id) throws Exception {
        return super.removeById(id);
    }
    @Override
    public List<Task> getToDoTask(int schoolID, String parcelCenterName, Date deadline,String workType) throws Exception {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(deadline);
        Date date = sdf.parse(str);

        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>()
                .eq(Task::getSchoolID,schoolID)
                .lt(Task::getSubmitTime,deadline)
                .gt(Task::getSubmitTime,date);

        if(workType.equals("取件员")){
            wrapper.eq(Task::getStatus, TaskStatusConstant.UNTAKE).eq(Task::getParcelCenterName,parcelCenterName).orderByAsc(Task::getCourierCompany);
        }else{
            wrapper.ge(Task::getStatus, TaskStatusConstant.TAKED_UNSEND).lt(Task::getStatus,TaskStatusConstant.COMPLETE)
                    .eq(Task::getDormitoryName,parcelCenterName).orderByAsc(Task::getFloor);
        }

        return taskMapper.selectList(wrapper);
    }

    @Override
    public List<Task> getToDoAndDoneTask(int schoolID, String parcelCenterName, Date deadline, String workType, String searchText) throws Exception {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(deadline);
        Date date = sdf.parse(str);

        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>()
                .eq(Task::getSchoolID,schoolID)
                .lt(Task::getSubmitTime,deadline)
                .gt(Task::getSubmitTime,date);

        if(workType.equals("取件员")){
            wrapper.le(Task::getStatus, TaskStatusConstant.TAKED_UNSEND).eq(Task::getParcelCenterName,parcelCenterName).orderByAsc(Task::getDormitoryName);
        }else{
            wrapper.ge(Task::getStatus, TaskStatusConstant.TAKED_UNSEND).eq(Task::getDormitoryName,parcelCenterName).orderByAsc(Task::getFloor);
        }

        if(!StringUtils.isEmpty(searchText)){
            wrapper.and(w->{w.like(Task::getAddress,searchText).or().like(Task::getPhone, searchText).or().like(Task::getPickupCode, searchText); });
        }


        return taskMapper.selectList(wrapper);
    }

    @Override
    public List<Task> getAllList(LambdaQueryWrapper<Task> wrapper) throws Exception{
        return taskMapper.selectList(wrapper);
    }



    @Override
    public TaskQueryVo getTaskById(Serializable id) throws Exception {
    return taskMapper.getTaskById(id);
    }

    @Override
    public Paging<TaskQueryVo> getTaskPageList(TaskPageParam taskPageParam) throws Exception {
        Page<TaskQueryVo> page = new PageInfo<>(taskPageParam, OrderItem.desc(getLambdaColumn(Task::getCreateTime)));
        IPage<TaskQueryVo> iPage = taskMapper.getTaskPageList(page, taskPageParam);
        return new Paging<TaskQueryVo>(iPage);
    }

}
